from utilities import *

from utilities import location_from_axis

from math import *

from py00439script_daemon import san_new_map, record_time_stamp, get_v, set_v, npc_set, npc_unset, npc_get, tsc, within_rect_by_corners

def encounter_exists( setup, encounter ):
	print "Testing encounter_exists"
	if (setup.flags & ES_F_SLEEP_ENCOUNTER):
		check = check_sleep_encounter(setup,encounter)
	else:
		check = check_random_encounter(setup,encounter)
	# Added by Sitra Achara
	if game.party[0].map == 5066 or game.party[0].map == 5067:
		# If you rest inside the Temple, execute the Reactive Temple scripts
		san_new_map(game.party[0], game.party[0])
	#f = open('re_setup3.txt','w')
	#f.write(str(locals()))
	#f.close()
	#f2 = open('re_setup2.txt','w')
	#f2.write(str(globals()))
	#f2.close()

	return check

def encounter_create( encounter ):
	print "Testing encounter_create with id=",encounter.id
	# encounter_create adds all the objects to the scene
	# WIP temp location for now

	target = game.leader
	if (encounter.id >= 4000 or encounter.id == 3000):
		location = game.leader.location
		range = 1
	else:
		location = Spawn_Point(encounter)
		range = 6
			
		numP = len(game.party) - 1
		xxx = game.random_range(0,numP)
		target = game.party[xxx]
		if (target == OBJ_HANDLE_NULL):
			target = game.party[0]
		
	if (encounter.map == 5078):
		Slaughtered_Caravan()
		
	if (encounter.map == 5072 and game.global_flags[855] == 1 and game.global_flags[875] == 0):
		game.global_flags[875] = 1
		flower = game.obj_create( 12900, location_from_axis (499L, 459L))

	is_camp = game.random_range(1,1) ## Added by Cerulean the Blue
	enemies = []
	i = 0
	total = len(encounter.enemies)
	while (i < total):
		j = 0
		while (j < encounter.enemies[i][1]):
#################################################################################################
## Random Encounter Encampment - idea and map by Shiningted, execution  by Cerulean the Blue
			if ( ( encounter.map == 5074 ) and not(is_daytime()) and ( is_camp == 1 ) ):
				target = OBJ_HANDLE_NULL
				encounter.location = location_from_axis( 470, 480 )
				location = location_from_axis( 505, 479)
				range = 6
#################################################################################################
				
			spawn_loc = random_location(location, range, target)
				
			if (encounter.id >= 4000):
				numP = len(game.party) - 1
				xxx = game.random_range(0, numP)
				target = game.party[xxx]
				location = target.location
				if (target == OBJ_HANDLE_NULL):
					target = game.party[0]
					location = game.party[0].location
				if game.leader.map == 5066:
				#scripting wonnilon's hideout
					legit_list = []
					barney = 0
					for moe in game.party:
						xx, yy = location_to_axis(moe.location)
						if xx > 423 or xx < 410 or yy > 390 or yy < 369:
							barney = 1
							legit_list.append(moe.location)
					if barney == 0:
						location = game.party[0].location
					else:
						xxx = game.random_range(0, len(legit_list) - 1)
						for moe in game.party:
							if moe.location == legit_list[xxx]:
								target = moe
						location = legit_list[xxx]
				spawn_loc = location
				
			npc = game.obj_create( encounter.enemies[i][0], spawn_loc )
			if (npc != OBJ_HANDLE_NULL):
				npc.turn_towards(target)
				if (game.leader.area == 1 and game.leader.reputation_has(1) == 1) or ((encounter.id < 2000) or (encounter.id >= 4000)):
					if target != OBJ_HANDLE_NULL:
						npc.attack(target)
					npc.npc_flag_set(ONF_KOS)
					enemies.append(npc)
				# m_count = m_count + 1
			j = j + 1
		i = i + 1
		
	return

def check_random_encounter( setup, encounter ):
	if (game.random_range(1,20) == 1):

		# encounter.location -- the location to teleport the player to
		r = game.random_range(1,3)
		if (r == 1):
			encounter.location = location_from_axis( 470, 480 )
		elif (r == 2):
			encounter.location = location_from_axis( 503, 478 )
		else:
			encounter.location = location_from_axis( 485, 485 )

		if (check_predetermined_encounter(setup,encounter)):
			return 1
		elif (check_unrepeatable_encounter(setup,encounter)):
			Survival_Check(encounter)
			return 1
		else: 
			check = check_repeatable_encounter(setup,encounter)
			Survival_Check(encounter)
			return check
	return 0

def check_sleep_encounter( setup, encounter ):
	NPC_Self_Buff()##	THIS WAS ADDED TO AID IN NPC SELF BUFFING			##

	## Revamped the chance system slightly. 
	## ran_factor determines chance of encounter. Translated to chance of encounter in an 8 hour rest period:
	## 10 - 56 percent
	## 11 - 60
	## 12 - 64
	## 13 - 67
	## 14 - 70
	## 15 - 72
	## 16 - 75
	## 17 - 77
	## 18 - 80
	## 19 - 81
	## 20 - 83
	## 21 - 85
	## 22 - 86
	## 23 - 87
	## 24 - 89
	## 25 - 90
	## 26 - 91
	## 27 - 91.9
	## 28 - 93
	## 29 - 93.5
	## 30 - 94.2
	## 31 - 94.8
	## 32 - 95.4
	## 33 - 95.9
	## 34 - 96.4
	## 35 - 96.8
	## 36 - 97.1
	##
	if game.leader.map == 5015 or game.leader.map == 5016 or game.leader.map == 5017:
	#resting in Burne's tower
		ran_factor = 31 
	elif game.leader.map == 5001 or game.leader.map == 5007:
	#resting in Hommlet Exterior
		ran_factor = 18 
	elif (game.leader.map == 5067 or game.leader.map == 5079 or game.leader.map == 5080 or game.leader.map == 5140) and game.global_flags[144] == 1:
	#resting in Temple on alert
		if game.leader.map == 5067 and (game.global_flags[105] == 0 or game.global_flags[106] == 0 or game.global_flags[107] == 0 or game.global_flags[139] == 0):
			ran_factor = 16
		elif (game.leader.map == 5079 or game.leader.map == 5080 or game.leader.map == 5140) and (game.global_flags[146] == 0 or game.global_flags[147] == 0):
			ran_factor = 21
	else: 
	#default - 56 percent chance of encounter in 8 hour rest
		ran_factor = 10
	if (game.random_range(1,100) <= ran_factor):
		encounter.id = 4000
		if (game.leader.area == 1):	# ----- Hommlet
			if (game.leader.reputation_has(29) == 1 or game.leader.reputation_has(30) == 1) and not (game.leader.reputation_has(1) == 1 or game.leader.reputation_has(32) == 1):
				#Slightly naughty
				enemy_list = ( ( 14371,1,2,1 ), )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.reputation_has(1) == 1 or game.leader.reputation_has(32) == 1) and get_v(439) < 9:
				#Moderately naughty
				enemy_list = ( ( 14371,2,4,1 ), )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.reputation_has(1) == 1 or game.leader.reputation_has(32) == 1) and get_v(439) >= 9 and get_v(439) <= 19:
				#Very Naughty
				num = game.random_range(4,7)
				temp = [ (14371, num ) ]
				if game.global_flags[336] == 0:
					temp.append( (14004,1) )
					#temp = (temp[0], (14004,1) )
					set_v(439, get_v(439) | 256 )
				if game.global_flags[437] == 0 :
					#p = len(temp)
					#if p == 2:
					#	temp = ( temp[0], temp[1], (14006,1), )
					#elif p == 1:
					#	temp = ( temp[0], (14006,1), )
					temp.append((14006,1))
					set_v(439, get_v(439) | 512 )
				if get_v(439) & 1024 == 0 :
					#p = len(temp)
					#if p == 3:
					#	temp = ( temp[0], temp[1], temp[2], (14012,1), )
					#elif p == 2:
					#	temp = ( temp[0], temp[1], (14012,1), )
					#elif p == 1:
					#	temp = ( temp[0], (14012,1), )
					temp.append((14012,1))
					set_v(439, get_v(439) | 1024)
				record_time_stamp(443)
				encounter.enemies = temp
				encounter.dc = 1
				return 1
			elif (game.leader.reputation_has(1) == 1 or game.leader.reputation_has(32) == 1) and get_v(439) >= 20 and game.time.time_game_in_seconds(game.time) < get_v(443) + 2*30*24*60*60:
				# NAUGHTINESS OVERWHELMING
				# You've exterminated all the badgers!
				# And not enough time has passed since you started massacring the badgers for a big revenge encounter
				return 0
			else:
				return 0
		elif (game.leader.area == 2):	# ----- Moat house
			if (game.leader.map == 5002):	# moathouse ruins
				# frog, tick, willowisp, wolf, crayfish, lizard, rat, snake, spider, brigand
				enemy_list = ( ( 14057,1,3,1 ), ( 14089,2,4,1 ), ( 14291,1,4,6 ), ( 14050,2,3,1 ), ( 14094,1,2,3 ), ( 14090,2,4,1 ), ( 14056,4,9,1 ), ( 14630,1,3,1 ), ( 14047,2,4,1 ), ( 14070,2,5,1 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5004):	# moathouse interior
				# rat, tick, lizard, snake, brigand
				enemy_list = ( ( 14056,4,9,1 ), ( 14089,2,4,1 ), ( 14090,2,4,1 ), ( 14630,1,3,1 ), ( 14070,2,5,1 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5005):	# moathouse dungeon
				# rat, lizard, zombie, bugbear, gnoll (unless gone), Lareth guard (unless Lareth killed or in group)
				enemy_list = ( ( 14056,4,9,1 ), ( 14090,2,4,1 ), ( 14092,1,3,1 ), ( 14093,1,3,2), ( 14067,1,3,1 ), ( 14074,2,4,1 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				if (x == 1):
					if (encounter.enemies[0][0] == 14067):	# check gnolls
						if (game.global_flags[288] == 1):
							return 0
					elif (encounter.enemies[0][0] == 14074):	# check Lareth
						if ((game.global_flags[37] == 1) or (game.global_flags[50] == 1)):
							return 0
				return x
			return 0
		elif (game.leader.area == 3):	# ----- Nulb
			return 0	# WIP, thieves?
		elif (game.leader.area == 4):	# ----- Temple
			if (game.leader.map == 5062):	# temple exterior
				# bandit, drelb (night only), rat, snake, spider
				enemy_list = ( ( 14070,2,5,1 ), ( 14275,1,1,4 ), ( 14056,4,9,1 ), ( 14630,1,3,1 ), ( 14047,2,4,1 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				if (x == 1):
					if (encounter.enemies[0][0] == 14275):	#check drelb
						if (is_daytime()):
							return 0
				return x
			elif (game.leader.map == 5064):	# temple interior
				# bandit, drelb (night only), rat, GT patrol
				enemy_list = ( ( 14070,2,5,1 ), ( 14275,1,1,4 ), ( 14056,4,9,1 ), ( 14170,2,5,2 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				if (x == 1):
					if (encounter.enemies[0][0] == 14275):	#check drelb
						if (is_daytime()):
							return 0
				return x
			elif (game.leader.map == 5065):	# temple tower
				# bandit, GT patrol
				enemy_list = ( ( 14070,2,5,1 ), ( 14170,2,5,2 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5066):	# temple dungeon 1
				enc_abort = 1
				for joe in game.party:
					xx, yy = location_to_axis(joe.location)
					if xx > 423 or xx < 410 or yy > 390 or yy < 369:
						enc_abort = 0
				if enc_abort == 1:
					return 0
				# bandit, gnoll, ghoul, gelatinous cube, gray ooze, ogre, GT patrol
				enemy_list = ( ( 14070,2,5,1 ), ( 14078,2,5,1 ), ( 14128,2,5,1 ), ( 14139,1,1,3 ), ( 14140,1,1,4 ), ( 14448,1,1,2 ), ( 14170,2,5,2 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5067):	# temple dungeon 2
				# bandit, bugbear, carrion crawler, ochre jelly, ogre, troll
				enemy_list = ( ( 14070,2,5,1 ), ( 14170,4,6,2 ), ( 14190,1,1,4 ), ( 14142,1,1,5 ), ( 14448,2,4,2 ), ( 14262,1,2,5 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5140):	# temple dungeon 3 lower
				# black pudding, ettin, gargoyle, hill giant, ogre, troll
				enemy_list = ( ( 14143,1,1,7 ), ( 14238,1,2,5 ), ( 14239,5,8,4 ), ( 14221,2,3,7 ), ( 14448,5,8,2 ), ( 14262,2,3,5 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5079):	# temple dungeon 3 upper
				return 0
			elif (game.leader.map == 5080):	# temple dungeon 4
				# black pudding, ettin, troll, gargoyle, hill giant, ogre + bugbear
				enemy_list = ( ( 14143,1,1,7 ), ( 14238,1,1,5 ), ( 14262,1,2,5 ), ( 14239,3,6,4 ), ( 14220,1,2,7 ), ( 14448,1,4,3 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				if (x == 1):
					if (encounter.enemies[0][0] == 14448):	# reinforce ogres with bugbears
						encounter.enemies.append( ( 14174, game.random_range(2,5) ) )
				return x
			elif (game.leader.map == 5081):	# air node
				# air elemental, ildriss grue, vapor rat, vortex, windwalker
				enemy_list = ( ( 14292,1,2,5 ), ( 14192,1,2,4 ), ( 14068,1,4,2 ), ( 14293,1,2,2 ), ( 14294,1,1,4 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5082):	# earth node
				# basilisk, chaggrin grue, crystal ooze, earth elemental
				enemy_list = ( ( 14295,1,1,5 ), ( 14191,1,4,4 ), ( 14141,1,1,4 ), ( 14296,1,2,5 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5083):	# fire node
				# fire bats, fire elemental, fire snake, fire toad
				enemy_list = ( ( 14297,2,5,2 ), ( 14298,1,2,5 ), ( 14299,1,2,1 ), ( 14300,1,2,3 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			elif (game.leader.map == 5084):	# water node
				# floating eye, ice lizard, lizard man, vodyanoi, water elemental, kopoacinth, lacedon, merrow
				enemy_list = ( ( 14301,1,1,1 ), ( 14109,1,1,3 ), ( 14084,2,4,1 ), ( 14261,1,1,7 ), ( 14302,1,2,5 ), ( 14240,2,3,4 ), ( 14132,3,5,1 ), ( 14108,3,5,2 ) )
				x = get_sleep_encounter_enemies(enemy_list,encounter)
				return x
			return 0
		else :
			party_level = group_average_level(game.leader)
			get_repeatable_encounter_enemies(setup,encounter)
			while (encounter.dc > (party_level+2)):
				get_repeatable_encounter_enemies(setup,encounter)
			encounter.id = 4000
			return 1
	return 0

def get_sleep_encounter_enemies( enemy_list, encounter ):
	total = len( enemy_list )
	n = game.random_range(0,total-1)	
	encounter.dc = enemy_list[n][3]
	party_level = group_average_level(game.leader)
	if (encounter.dc > (party_level+2)):
		# try again
		n = game.random_range(0,total-1)	
		encounter.dc = enemy_list[n][3]
		if (encounter.dc > (party_level+2)):
			return 0
	num = game.random_range(enemy_list[n][1],enemy_list[n][2])
	encounter.enemies = ( ( enemy_list[n][0], num ), )
	
	return 1

def check_predetermined_encounter( setup, encounter ):
	while (len(game.encounter_queue) > 0):
		id = game.encounter_queue[0]
		del game.encounter_queue[0]
		if (game.global_flags[id - 3000 + 277] == 0):
			game.global_flags[id - 3000 + 277] = 1
			encounter.id = id
			encounter.dc = 1000	# unavoidable
			encounter.map = get_map_from_terrain(setup.terrain)
			if (id == 3000):
				encounter.enemies = ( ( 14303, 1 ), )
			elif (id == 3001):
				encounter.enemies = ( ( 14307, 1 ), ( 14308, 10 ) )
			elif (id == 3002):
				encounter.enemies = ( ( 14305, 1 ), ( 14306, 6 ) )
			elif (id == 3003):
				encounter.enemies = ( ( 14304, 1 ), )
			elif (id == 3010):
				encounter.enemies = ( ( 14694, 1 ), )
			elif (id == 3579):
				encounter.enemies = ( ( 14066, 1 ), ( 14078, 3 ), ( 14079, 3 ), ( 14080, 3 ), ( 14067, 3 ) )
			elif (id == 3605 and is_daytime() == 1):
				encounter.location = location_from_axis( 465, 475 )
				encounter.map = 5078
				encounter.enemies = ( ( 14459, 1 ),  )
			elif (id == 3605 and is_daytime() == 0):
				game.global_flags[882] = 0
				game.encounter_queue.append(3605)
				return 0
			elif (id == 3004):
				if (game.global_flags[202] == 1):
					encounter.enemies = ( ( 14315, 4 ), )
				else:
					return 0
			elif (id == 3159):
			# Following the trader's tracks, you find them camping
				encounter.enemies = ( (14014,1), (14018,1) )
				set_v(437,101)
				encounter.map = 5074
			else:
				return 0
			return 1
	return 0

def check_unrepeatable_encounter( setup, encounter ):
	#make some fraction of all encounters an unrepeatable encounter
	if (game.random_range(1,10) == 1):
		id = game.random_range(2000,2002)
		if (game.global_flags[id - 2000 + 227] == 0):
			encounter.id = id
			encounter.map = get_map_from_terrain(setup.terrain)
			if (id == 2000):
				encounter.dc = 9
				encounter.enemies = ( ( 14142, 4 ), )
			elif (id == 2001):
				encounter.dc = 5
				encounter.enemies = ( ( 14331, 1 ), )
			elif (id == 2002):
				encounter.dc = 9
				encounter.enemies = ( ( 14332, 1 ), ( 14333, 1 ), ( 14334, 1 ), ( 14335, 1 ), ( 14336, 1 ), ( 14622, 1))
			else:
				return 0
			party_level = group_average_level(game.leader)
			if (encounter.dc > (party_level+2)):
				return 0
			game.global_flags[id - 2000 + 227] = 1
			return 1

	return 0

def check_repeatable_encounter( setup, encounter ):
	encounter.map = get_map_from_terrain(setup.terrain)
	if ( ( encounter.map == 5074 ) and not(is_daytime()) ):
		encounter.location = location_from_axis( 470, 480 )
	if (encounter.map == 5072 or encounter.map == 5076):
		encounter.location = location_from_axis( 485, 485 )
	party_level = group_average_level(game.leader)
	get_repeatable_encounter_enemies(setup,encounter)
	while (encounter.dc > (party_level+2)):
		get_repeatable_encounter_enemies(setup,encounter)
	return 1

## NEW MAP GENERATOR BY CERULEAN THE BLUE
def get_map_from_terrain(terrain):
	map = 5069
	if (map == 5069):
		map = 5069 + game.random_range(1,8)
	return map

## NEW RANDOM ENCOUNTER ENEMY GENERATOR BY CERULEAN THE BLUE
def get_repeatable_encounter_enemies( setup, encounter ):
	if ((encounter.map == 5070) or (encounter.map == 5074)):
		if (is_daytime()):
			get_scrub_daytime(encounter)
		else: 
			get_scrub_nighttime(encounter)
	elif ((encounter.map == 5071) or (encounter.map == 5075)):
		if (is_daytime()):
			get_forest_daytime(encounter)
		else:
			get_forest_nighttime(encounter)
	elif ((encounter.map == 5072) or (encounter.map == 5076)):
		if (is_daytime()):
			get_swamp_daytime(encounter)
		else:
			get_swamp_nighttime(encounter)
	else:	# TERRAIN_RIVERSIDE
		if (is_daytime()):
			get_riverside_daytime(encounter)
		else:
			get_riverside_nighttime(encounter)
	return
## END NEW RANDOM ENCOUNTER ENEMY GENERATOR

def get_scrub_daytime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,11)
	if (r == 1):
		# bandits
		n = game.random_range(2,5)
		encounter.dc = 2 + (n/2)
		encounter.enemies = ( ( 14069, 1*m ), ( 14070, n*m ) )
		encounter.id =  1000
	elif (r == 2):
		# ettin and hobgoblins
		n = game.random_range(3,6)
		encounter.dc = 6
		encounter.enemies = ( ( 14238, 1*m ), ( 14188, n*m ) )
		encounter.id =  1039
	elif (r == 3):
		# ettin and brown bears
		n = game.random_range(1,2)
		encounter.dc = 7
		encounter.enemies = ( ( 14238, 1*m ), ( 14053, n*m ) )
		encounter.id =  1040
	elif (r == 4):
		# ettins
		n = game.random_range(1,4)
		encounter.dc = 4+n
		encounter.enemies = ( ( 14069, n*m ), )
		encounter.id =  1041
	elif (r == 5):
		# gnolls
		n = game.random_range(2,4)
		encounter.dc = 3
		encounter.enemies = ( ( 14067, n*m ), )
		encounter.id =  1018
	elif (r == 6):
		# gnolls and wolves
		n = game.random_range(1,3)
		n2 = game.random_range(1,3)
		encounter.dc = 4
		encounter.enemies = ( ( 14067, n*m ), ( 14050, n2*m ) )
		encounter.id =  1019
	elif (r == 7):
		# goblins
		n = game.random_range(3,6)
		encounter.dc = 1
		encounter.enemies = ( ( 14184, n*m ), )
		encounter.id =  1003
	elif (r == 8):
		# goblins and wolves
		n = game.random_range(3,6)
		n2 = game.random_range(1,3)
		encounter.dc = 3
		encounter.enemies = ( ( 14184, n*m ), ( 14050, n2*m ) )
		encounter.id =  1004
	elif (r == 9):
		# hill giants
		n = game.random_range(1,4)
		encounter.dc = 6+n
		encounter.enemies = ( ( 14217, n*m ), )
		encounter.id =  1045
	elif (r == 10):
		# kobolds and kobold sergeant
		n = game.random_range(3,10)
		encounter.dc = 1
		encounter.enemies = ( ( 14640, n*m ), (14641, 1*m) )
		encounter.id =  1003
	else:
		# jackals
		n = game.random_range(2,4)
		encounter.dc = 1
		encounter.enemies = ( ( 14051, n*m ), )
		encounter.id =  1006
	return

def get_scrub_nighttime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,11)
	if (r == 1):
		# bugbears and goblins
		n = game.random_range(3,6)
		encounter.dc = 3
		encounter.enemies = ( ( 14093, 1*m ), ( 14184, n*m ) )
		encounter.id =  1026
	elif (r == 2):
		# bugbears and hobgoblins
		n = game.random_range(4,9)
		encounter.dc = 4
		encounter.enemies = ( ( 14093, 1*m ), ( 14188, n*m ) )
		encounter.id =  1027
	elif (r == 3):
		# bugbears
		n = game.random_range(2,4)
		encounter.dc = 1+n
		encounter.enemies = ( ( 14093, n*m ), )
		encounter.id =  1028
	elif (r == 4):
		# bugbears and wolves
		n = game.random_range(2,4)
		n2 = game.random_range(2,4)
		encounter.dc = 7
		encounter.enemies = ( ( 14093, n*m ), ( 14050, n2*m ) )
		encounter.id =  1029
	elif (r == 5):
		# gnolls
		n = game.random_range(2,4)
		encounter.dc = 3
		encounter.enemies = ( ( 14067, n*m ), )
		encounter.id =  1018
	elif (r == 6):
		# gnolls and wolves
		n = game.random_range(1,3)
		n2 = game.random_range(1,3)
		encounter.dc = 4
		encounter.enemies = ( ( 14067, n*m ), ( 14050, n2*m ) )
		encounter.id =  1019
	elif (r == 7):
		# goblins
		n = game.random_range(3,6)
		encounter.dc = 1
		encounter.enemies = ( ( 14184, n*m ), )
		encounter.id =  1003
	elif (r == 8):
		# goblins and wolves
		n = game.random_range(3,6)
		n2 = game.random_range(1,3)
		encounter.dc = 3
		encounter.enemies = ( ( 14184, n*m ), ( 14050, n2*m ) )
		encounter.id =  1004
	elif (r == 9):
		#zombies
		n = game.random_range(1,3)
		encounter.dc = 1
		encounter.enemies = ( ( 14092, n*m ), )
		encounter.id =  1014
	elif (r == 10):
		# kobolds and kobold sergeant
		n = game.random_range(3,12)
		encounter.dc = 1
		encounter.enemies = ( ( 14640, n*m ), (14641, 1*m ) )
		encounter.id =  1003
	else:
		#zombies
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14092, n*m ), )
		encounter.id =  1015
	return

def get_forest_daytime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,14)
	if (r == 1):
		# bandits
		n = game.random_range(2,5)
		encounter.dc = 2 + (n/2)
		encounter.enemies = ( ( 14069, 1*m ), ( 14070, n*m ) )
		encounter.id =  1000
	elif (r == 2):
		# black bears
		n = game.random_range(1,3)
		encounter.dc = 3
		encounter.enemies = ( ( 14052, n*m ), )
		encounter.id =  1025
	elif (r == 3):
		# brown bears
		n = game.random_range(1,3)
		encounter.dc = 5
		encounter.enemies = ( ( 14053, n*m ), )
		encounter.id =  1036
	elif (r == 4):
		# harpies
		n = game.random_range(1,3)
		encounter.dc = 5
		encounter.enemies = ( ( 14243, n*m ), )
		encounter.id =  1037
	elif (r == 5):
		# hobgoblin and goblins
		n = game.random_range(1,3)
		encounter.dc = 1
		encounter.enemies = ( ( 14188, 1*m ), ( 14184, n*m ) )
		encounter.id =  1009
	elif (r == 6):
		# hobgoblins
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14188, n*m ), )
		encounter.id =  1010
	elif (r == 7):
		# hobgoblin and goblins
		n = game.random_range(1,3)
		n2 = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14188, n*m ), ( 14184, n2*m ) )
		encounter.id =  1011
	elif (r == 8):
		# hobgoblin and wolves
		n = game.random_range(1,3)
		n2 = game.random_range(1,3)
		encounter.dc = 3
		encounter.enemies = ( ( 14188, n*m ), ( 14050, n2*m ) )
		encounter.id =  1012
	elif (r == 9):
		# ogres
		n = game.random_range(2,4)
		encounter.dc = n
		encounter.enemies = ( ( 14448, n*m ), )
		encounter.id =  1031
	elif (r == 10):
		# owlbears
		n = game.random_range(1,3)
		encounter.dc = 3+n
		encounter.enemies = ( ( 14046, n*m ), )
		encounter.id =  1038
	elif (r == 11):
		# large spiders
		n = game.random_range(2,4)
		encounter.dc = n
		encounter.enemies = ( ( 14047, n*m ), )
		encounter.id =  1030
	elif (r == 12):
		# stirges
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14182, n*m ), )
		encounter.id =  1013
	elif (r == 13):
		# giant ticks
		n = game.random_range(2,4)
		encounter.dc = n
		encounter.enemies = ( ( 14089, n*m ), )
		encounter.id =  1024
	else:
		# wolves
		n = game.random_range(2,3)
		encounter.dc = 2
		encounter.enemies = ( ( 14050, n*m ), )
		encounter.id =  1022
	return

def get_forest_nighttime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,6)
	if (r == 1):
		# hobgoblin and goblins
		n = game.random_range(1,3)
		encounter.dc = 1
		encounter.enemies = ( ( 14188, 1*m ), ( 14184, n*m ) )
		encounter.id =  1009
	elif (r == 2):
		# hobgoblins
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14188, n*m ), )
		encounter.id =  1010
	elif (r == 3):
		# hobgoblin and goblins
		n = game.random_range(1,3)
		n2 = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14188, n*m ), ( 14184, n2*m ) )
		encounter.id =  1011
	elif (r == 4):
		# hobgoblin and wolves
		n = game.random_range(1,3)
		n2 = game.random_range(1,3)
		encounter.dc = 3
		encounter.enemies = ( ( 14188, n*m ), ( 14050, n2*m ) )
		encounter.id =  1012
	elif (r == 5):
		# stirges
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14182, n*m ), )
		encounter.id =  1013
	else:
		#zombies
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14092, n*m ), )
		encounter.id =  1015
	return

def get_swamp_daytime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,10)
	if (r == 1):
		# crayfish
		n = game.random_range(1,2)
		encounter.dc = 2+n
		encounter.enemies = ( ( 14094, n*m ), )
		encounter.id =  1032
	elif (r == 2):
		# frogs
		n = game.random_range(1,3)
		encounter.dc = 2
		encounter.enemies = ( ( 14057, n*m ), )
		encounter.id =  1016
	elif (r == 3):
		# lizards
		n = game.random_range(2,4)
		encounter.dc = n
		encounter.enemies = ( ( 14090, n*m ), )
		encounter.id =  1023
	elif (r == 4):
		# lizardmen
		n = game.random_range(2,3)
		encounter.dc = 2
		encounter.enemies = ( ( 14084, n*m ), )
		encounter.id =  1020
	elif (r == 5):
		# lizardmen with lizard
		n = game.random_range(1,3)
		encounter.dc = 4
		encounter.enemies = ( ( 14084, n*m ), ( 14090 , 1*m ) )
		encounter.id =  1021
	elif (r == 6):
		# rats
		n = game.random_range(4,9)
		encounter.dc = 1
		encounter.enemies = ( ( 14056, n*m ), )
		encounter.id =  1002
	elif (r == 7):
		# snakes
		n = game.random_range(1,3)
		encounter.dc = 3 + (n/2)
		encounter.enemies = ( ( 14630, n*m ), )
		encounter.id =  1033
	elif (r == 8):
		# stirges
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14182, n*m ), )
		encounter.id =  1013
	elif (r == 9):
		# giant ticks
		n = game.random_range(2,4)
		encounter.dc = n
		encounter.enemies = ( ( 14089, n*m ), )
		encounter.id =  1024
	else:
		# trolls
		n = game.random_range(1,4)
		encounter.dc = 4+n
		encounter.enemies = ( ( 14262, n*m ), )
		encounter.id =  1042
	return

def get_swamp_nighttime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,7)
	if (r == 1):
		# ghasts and ghouls
		n = game.random_range(2,4)
		encounter.dc = 5
		encounter.enemies = ( ( 14135, 1*m ), ( 14128, n*m ) )
		encounter.id =  1034
	elif (r == 2):
		# ghouls
		n = game.random_range(1,3)
		encounter.dc = 1 + (n/2)
		encounter.enemies = ( ( 14128, n*m ), )
		encounter.id =  1017
	elif (r == 3):
		# groaning spirit
		encounter.dc = 7
		encounter.enemies = ( ( 14280, 1*m ), )
		encounter.id =  1044
	elif (r == 4):
		# black bears
		n = game.random_range(2,5)
		encounter.dc = 2 + (n/2)
		encounter.enemies = ( ( 14052, n*m ), )
		encounter.id =  1035
	elif (r == 5):
		# stirges
		n = game.random_range(3,6)
		encounter.dc = 2
		encounter.enemies = ( ( 14182, n*m ), )
		encounter.id =  1013
	elif (r == 6):
		# trolls
		n = game.random_range(1,4)
		encounter.dc = 4+n
		encounter.enemies = ( ( 14262, n*m ), )
		encounter.id =  1042
	else:
		# willowisps
		n = game.random_range(1,4)
		encounter.dc = 5+n
		encounter.enemies = ( ( 14291, n*m ), )
		encounter.id =  1043
	return

def get_riverside_daytime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,6)
	if (r == 1):
		# crayfish
		n = game.random_range(1,2)
		encounter.dc = 2+n
		encounter.enemies = ( ( 14094, n*m ), )
		encounter.id =  1032
	elif (r == 2):
		# lizards
		n = game.random_range(2,4)
		encounter.dc = n
		encounter.enemies = ( ( 14090, n*m ), )
		encounter.id =  1023
	elif (r == 3):
		# lizardmen
		n = game.random_range(2,3)
		encounter.dc = 2
		encounter.enemies = ( ( 14084, n*m ), )
		encounter.id =  1020
	elif (r == 4):
		# lizardmen with lizard
		n = game.random_range(1,3)
		encounter.dc = 4
		encounter.enemies = ( ( 14084, n*m ), ( 14090 , 1*m ) )
		encounter.id =  1021
	elif (r == 5):
		# pirates
		n = game.random_range(2,5)
		encounter.dc = 1 + (n/2)
		encounter.enemies = ( ( 14290, n*m ), )
		encounter.id =  1001
	else:
		# trolls
		n = game.random_range(1,4)
		encounter.dc = 4+n
		encounter.enemies = ( ( 14262, n*m ), )
		encounter.id =  1042
	return

def get_riverside_nighttime( encounter ):
	m = Get_Multiplier(encounter)
	m2 =1
	if m > 1:
		m2 = m - 1
	r = game.random_range(1,4)
	if (r == 1):
		# lacedon (ghouls)
		n = game.random_range(1,3)
		encounter.dc = 1 + (n/2)
		encounter.enemies = ( ( 14130, n*m ), )
		encounter.id =  1017
	elif (r == 2):
		# skeleton gnolls
		n = game.random_range(2,4)
		encounter.dc = 1
		encounter.enemies = ( ( 14081, n*m ), )
		encounter.id =  1007
	elif (r == 3):
		# skeletons
		n = game.random_range(2,4)
		encounter.dc = 1
		encounter.enemies = ( ( 14107, n*m ), )
		encounter.id =  1008
	else:
		# trolls
		n = game.random_range(1,4)
		encounter.dc = 4+n
		encounter.enemies = ( ( 14262, n*m ), )
		encounter.id =  1042
	return

def can_sleep():
	# can_sleep is called to test the safety of sleeping
	# it must return one of the following
	#   SLEEP_SAFE
	#      * it is totally safe to sleep
	#   SLEEP_DANGEROUS
	#      * it may provoke a random encounter
	#   SLEEP_IMPOSSIBLE
	#      * rest is not possible here
	#   SLEEP_PASS_TIME_ONLY
	#      * resting here actually only passes time, no healing or spells are retrieved
	if (game.leader.area == 1):	# Hommlet
		if (game.party[0].reputation_has(1) == 1 or game.party[0].reputation_has(29) == 1 or game.party[0].reputation_has(30) == 1 or game.party[0].reputation_has(32) == 1) and game.party[0].map != 5014:
			return SLEEP_DANGEROUS
		if ((game.leader.map == 5007) or (game.leader.map == 5008)):	# inn first or second floor
			if ((game.global_flags[56] == 1) or (game.quests[18].state == qs_completed)):
				return SLEEP_SAFE
		return SLEEP_PASS_TIME_ONLY
	elif (game.leader.map == 5065 and game.global_flags[840] == 1):	# bandit hideout
			return SLEEP_SAFE
	elif (game.leader.map == 5066):	# wonnilon hideout
		#if game.global_flags[404] == 1:
		#	return SLEEP_SAFE
		#else:
			return SLEEP_DANGEROUS
	elif (game.leader.area == 2):	# Moat house
		if (game.leader.map == 5003):	# tower
			return SLEEP_SAFE
		return SLEEP_DANGEROUS
	elif (game.leader.area == 3):	# Nulb
		if ((game.leader.map == 5085) and (game.global_flags[94] == 1)):
			return SLEEP_SAFE
		elif (((game.leader.map == 5060) or (game.leader.map == 5061)) and (game.global_flags[289] == 1)):
			return SLEEP_SAFE	# WIP, thieves?
		return SLEEP_PASS_TIME_ONLY
	elif (game.leader.area == 4):	# Temple
		return SLEEP_DANGEROUS
	elif ((game.leader.map >= 5096) and (game.leader.map <= 5104)):	# Vignettes
		return SLEEP_IMPOSSIBLE
	elif game.leader.map == 5116 or game.leader.map == 5118: # Tutorial maps 1 & 3
		return SLEEP_IMPOSSIBLE
	elif game.leader.map == 5117: # Tutorial map 2
		return SLEEP_SAFE
	elif ((game.leader.map == 5121) or (game.leader.map == 5122) or (game.leader.map == 5132)  or (game.leader.map == 5133) or (game.leader.map == 5134) or (game.leader.map == 5135) or (game.leader.map == 5136) or (game.leader.map == 5147) or (game.leader.map == 5148) or (game.leader.map == 5149) or (game.leader.map == 5150) or (game.leader.map == 5151) or (game.leader.map == 5152) or (game.leader.map == 5153) or (game.leader.map == 5154) or (game.leader.map == 5155) or (game.leader.map == 5156) or (game.leader.map == 5159)): #Verbobonc maps by Allyx
		return SLEEP_PASS_TIME_ONLY
	elif game.leader.map == 5123: # ShopMap
		return SLEEP_SAFE
	elif ((game.leader.map == 5124) or (game.leader.map == 5125)): #Verbobonc Inn by Allyx
		if (game.global_flags[997] == 1):
			return SLEEP_SAFE
		return SLEEP_PASS_TIME_ONLY
	elif game.leader.map == 5160: # Mona's Store
		return SLEEP_PASS_TIME_ONLY
	elif game.leader.map == 5119: # Arena of Heroes
		return SLEEP_PASS_TIME_ONLY
	return SLEEP_DANGEROUS

#######################################################################
##  Random Encounter Check tweaking my Cerulean the Blue
#######################################################################

def Survival_Check(encounter):
	if encounter.dc < 1000:
		PC_roll = game.random_range(1,20)
		NPC_roll = game.random_range(1,20)
		PC_mod = PC_Modifier()
		NPC_mod = NPC_Modifier(encounter)
		print PC_roll, ' + ', PC_mod/3, ' vs ', NPC_roll, ' + ', NPC_mod/3
		if PC_roll + (PC_mod/3) >= NPC_roll + (NPC_mod/3):
			encounter.dc = 1
		else:
			encounter.dc = 1000
		game.global_vars[35] = NPC_roll + NPC_mod/3 - (PC_roll + PC_mod/3)
	return 1

def PC_Modifier():
	high = 0
	level = 0
	wild = 0
	listen = 0
	spot = 0
	for obj in game.party[0].group_list():
		listen = obj.skill_level_get(skill_listen)
		spot = obj.skill_level_get(skill_spot)
		wild = obj.skill_level_get(skill_wilderness_lore)
		level = spot + listen + wild
		if level > high:
			high = level
	return high

def NPC_Modifier(encounter):
	high = 0
	level = 0
	wild = 0
	listen = 0
	spot = 0
	for i in encounter.enemies:
		obj = game.obj_create(i[0], game.party[0].location)
		listen = obj.skill_level_get(skill_listen)
		print 'NPC ', i, ' Listen = ', listen
		spot = obj.skill_level_get(skill_spot)
		print 'NPC ', i, ' Spot = ', spot
		wild = obj.skill_level_get(skill_wilderness_lore)
		print 'NPC ', i, ' Wild = ', wild
		level = spot + listen + wild
		if level > high:
			high = level
		obj.destroy()
	return high
	

def Spawn_Point(encounter):
	diff = game.global_vars[35]
	distance = 10 - diff
	if distance < 0:
		distance = 0
	if distance > 15:
		distance = 15
	print 'Distance = ', distance
		
	p_list = get_circle_point_list( game.party[0].location, distance, 16)
		
	return p_list[game.random_range(0, len(p_list) - 1)]

def random_location(loc, range, target):
	print 'Generating Location'
	x, y = location_to_axis(loc)
	t_x, t_y = location_to_axis(target.location)
	rand_x = game.random_range(1,3)
	rand_y = game.random_range(1,3)
	loc_x = t_x
	loc_y = t_y
		
	while sqrt( (loc_x - t_x)**2 + (loc_y - t_y)**2 ) < sqrt( (x - t_x)**2 + (y - t_y)**2 ):
		if rand_x == 1:
			loc_x = ( x + game.random_range(1,range) )
		elif rand_x == 2:
			loc_x = ( x - game.random_range(1,range) )
		else:
			loc_x = x
			
		if rand_y == 1:
			loc_y = ( y + game.random_range(1,range) )
		elif rand_y == 2:
			loc_y = ( y - game.random_range(1,range) )
		else:
			loc_y = y
			
		rand_x = game.random_range(1,3)
		rand_y = game.random_range(1,3)
		
	location = location_from_axis( loc_x, loc_y )
	return location

def group_skill_level( pc, skill ):
	high = 0
	level = 0
	for obj in pc.group_list():
		level = obj.skill_level_get( skill )
		if (level > high):
			high = level

	if anyone( game.party[0].group_list(), "has_item", 12677):
		# If your party has the spyglass, double the roll!
		high = high*2
	if (high == 0):

		return 1
	return high



def Get_Multiplier(encounter):
	m_range = (group_average_level(game.leader)/4)
	if m_range < 1:
		m_range = 1
	multiplier = game.random_range(1,m_range)
	if (  encounter.map != 5074  or is_daytime() ):
		chance = 40/group_average_level(game.leader)
		if ( game.random_range(1, chance) != 1 ):
			multiplier = 1
	if (encounter.id >= 2000):
		multipler = 1
	return multiplier

##############################################################

def get_circle_point_list(center, radius,num_points): # By Darmagon
	p_list=[]
	offx, offy = location_to_axis(center)
	i = 0.00
	while i < 2*pi:
		posx = int(cos(i)*radius)+offx
		posy = int(sin(i)*radius)+offy
		loc = location_from_axis(posx,posy)
		p_list.append(loc)
		i = i + pi/(num_points/2)
	return p_list

def Slaughtered_Caravan(): # By Livonya
	dead = game.obj_create( 2112, location_from_axis (477L, 484L))	
	dead.rotation = game.random_range(1,20)
	dead = game.obj_create( 2118, location_from_axis (486L, 462L))
	dead.rotation = game.random_range(1,20)
	dead = game.obj_create( 2125, location_from_axis (465L, 475L))
	dead.rotation = game.random_range(1,20)
	dead = game.obj_create( 2112, location_from_axis (477L, 467L))	
	dead.rotation = game.random_range(1,20)
	dead = game.obj_create( 2112, location_from_axis (481L, 473L))
	dead.rotation = game.random_range(1,20)
	dead = game.obj_create( 2125, location_from_axis (489L, 498L))
	dead.rotation = game.random_range(1,20)
	chest = game.obj_create( 1004, location_from_axis (482L, 465L))	
	chest.rotation = 2
	dead = game.obj_create( 2118, location_from_axis (476L, 506L))	
	dead.rotation = game.random_range(1,20)
	ticker = game.obj_create( 14638, location_from_axis (465, 475 ))
	tree = game.obj_create( 2017, location_from_axis (474L, 455L))	
	tree.rotation = game.random_range(1,20)
	tree = game.obj_create( 2017, location_from_axis (504L, 489L))	
	tree.rotation = game.random_range(1,20)
	tree = game.obj_create( 2017, location_from_axis (474L, 496L))	
	tree.rotation = game.random_range(1,20)
	tree = game.obj_create( 2017, location_from_axis (471L, 469L))	
	tree.rotation = game.random_range(1,20)
	return 1

def NPC_Self_Buff(): # By Livonya
##
##	THIS WAS ADDED TO AID IN NPC SELF BUFFING			##
##										##
	for i in range(712, 733):
		print i
		game.global_vars[i] = 0
	return 1
##										##
##	THIS WAS ADDED TO AID IN NPC SELF BUFFING			##
##

